package controllers;

import static play.data.Form.form;
import models.Employee;
import models.LegalEntity;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;

import com.apina.views.html.crud.employee.createForm;
import com.apina.views.html.crud.employee.editForm;
import com.apina.views.html.crud.employee.list;

public class EmployeeCRUD extends Controller {
	
    public static Result GO_HOME = redirect(
            routes.EmployeeCRUD.list(0, "firstname", "asc", "")
    );
	
	  /**
     * Display the paginated list of Employees.
     *
     * @param page Current page number (starts from 0)
     * @param sortBy Column to be sorted
     * @param order Sort order (either asc or desc)
     * @param filter Filter applied on Employee names
     */
    public static Result list(int page, String sortBy, String order, String filter) {
        return ok(
            list.render(
                Employee.page(page, 10, sortBy, order, filter),
                sortBy, order, filter
            )
        );
    }
    
    /**
     * Display the 'edit form' of a existing Employee.
     *
     * @param id Id of the Employee to edit
     */
    public static Result edit(Long id) {
        Form<Employee> EmployeeForm = form(Employee.class).fill(
            Employee.find.byId(id)
        );
        return ok(
            editForm.render(id, EmployeeForm,LegalEntity.find.all())
        );
    }
    
    /**
     * Handle the 'edit form' submission 
     *
     * @param id Id of the Employee to edit
     */
    public static Result update(Long id) {
        Form<Employee> EmployeeForm = form(Employee.class).bindFromRequest();
        if(EmployeeForm.hasErrors()) {
            return badRequest(editForm.render(id, EmployeeForm,LegalEntity.find.all()));
        }
        EmployeeForm.get().update(id);
        flash("success", "Employee " + EmployeeForm.get().firstname + " has been updated");
        return GO_HOME;
    }
    
    /**
     * Display the 'new Employee form'.
     */
    public static Result create() {
        Form<Employee> EmployeeForm = form(Employee.class);
        return ok(
            createForm.render(EmployeeForm,LegalEntity.find.all())
        );
    }
    
    /**
     * Handle the 'new Employee form' submission 
     */
    public static Result save() {
        Form<Employee> EmployeeForm = form(Employee.class).bindFromRequest();
        if(EmployeeForm.hasErrors()) {
            return badRequest(createForm.render(EmployeeForm,LegalEntity.find.all()));
        }
        EmployeeForm.get().save();
        flash("success", "Employee " + EmployeeForm.get().firstname + " has been created");
        return GO_HOME;
    }
    
    /**
     * Handle Employee deletion
     */
    public static Result delete(Long id) {
        Employee.find.ref(id).delete();
        flash("success", "Employee has been deleted");
        return GO_HOME;
    }

}
